#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;

using i64 = long long;

int a[N], ind[N];
string str[N];

int check(int u, string s) { // function<int(int, string)>
    if(ind[u] == 0) return (s[0] == 'A') + (s[1] == 'B') + (s[2] == 'C') + (s[3] == 'D') + (s[4] == 'E');
    ind[u] = 0;
    string t(5, '?');
    for(int i = 0; i < 5; i ++)
        t[i] = str[u][(s[i] - 'A')];
    return check(a[u], t);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin >> n;
    
    for(int i = 1; i <= n; i ++) {
        cin >> a[i] >> str[i];
        ind[a[i]] ++;
    }
    
    queue<int> q;
    for(int i = 1; i <= n; i ++)
        if(ind[i] == 0) q.push(i);
    
    while(q.size()) {
        auto u = q.front();
        q.pop();
        if(--ind[a[u]] == 0) q.push(a[u]);
    }
    
    i64 ans = 1;
    for(int i = 1; i <= n; i ++)
        if(ind[i]) (ans *= check(i, "ABCDE")) %= 998244353;
    
    
    cout << ans;
}